﻿
<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>修改计划用料</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <script src="~/js/layui/layui.js"></script>
    <link href="~/js/layui/css/layui.css" rel="stylesheet" />
    <link href="~/X-admin/css/xadmin.css" rel="stylesheet" />
    <script src="~/X-admin/js/jquery.min.js"></script>
    <script src="~/X-admin/js/xadmin.js"></script>
    <script src="~/js/framework/sysframework.js"></script>
    <script src="~/js/framework/commonjs.js"></script>
</head>
<body>
    <ul lay-bar="disabled" class="mWebBanner">
        <li class="layui-nav-item" style="float:left;">
            <button id="btn-sync" type="button" onclick="closeWin()" class="buttonCss">
                <i class="layui-icon" style="margin-right:3px">&#x1007;</i> 关闭页面
            </button>
        </li>
    </ul>
    <div class="layui-card" style="margin-top:10px">
        <div class="layui-card-body" style="padding-top:4%">
            <form id="dForm" class="layui-form layui-col-space5">
  
                <div class="layui-form-item">
                    <div class="layui-input-group" style="float:left;">
                        <div class="layui-input-prefix">
                            关联计划
                        </div>
                        <input type="text" id="plCode" name="plCode" class="layui-input"  readonly >
                    </div>
                </div>
              

            </form>
        </div>
        <div class="layui-card-body" id="layui-table-main" >
            <table class="layui-hide" id="list" lay-filter="list"></table>  
        </div>
    </div>
</body>
</html>
<script src="~/js/sysweb/planjs.js"></script>
<script type="text/html" id="barDemo">
    <a title="删除" href="javascript:;" lay-event="del" ><i class="layui-icon">&#xe640;</i></a>
</script>
<script type="text/javascript">

    var plCode = ""; var proCodes = "";

    $(function () {
        
        plCode = getUrlVariable("plCode");
        $("input[ name='plCode']").val(plCode);

        loadPro();

    });

    
    var dUrl = "../Plan/getDataList?dataType=planyl&condition=" + getUrlVariable("plCode");
    function loadPro(){
        layui.use('table', function () {
            var table = layui.table;
            var cols = table.render({
                elem: '#list'
                //, data: JSON.parse(datas)
                , url: dUrl
                , skin: 'row' //表格风格
                , even: true
                , height: 'full-200'   //固定高度-即固定表头固定第一行首行
                , page: false
                , limits: [50, 70, 100]
                , limit: 50 //每页默认显示的数量
                , cols: [[
                    { field: 'proCode', title: '原材料编码', width: 120 ,event:"addPro"}
                    , { field: 'proName', title: '原材料名称', width: 180, event: "addPro" }
                    , { field: 'batch', title: '批次', width: 180}
                    , { field: 'cjStock', title: '车间库存', width: 120}
                    , { field: 'number', title: '使用数量', width: 120, edit: 'text' }
                    // , { field: 'uPrice', title: '单价', width: 120 }
                    // , { field: 'total', title: '总金额', width: 120}
                    , { field: 'right', align: 'center', title: '', toolbar: '#barDemo', width: 90 }
                ]], done: function (data) {


                }
            }).config.cols;

            //监听工具条
            table.on('tool(list)', function (obj) {
                var data = obj.data; //当前行旧数据
                var field = obj.field; //单元格字段
                var value = obj.value; //修改后的值
                var event = obj.event; //当前单元格事件属性值
                var update = {};
                update[field] = value;
                //把value更新到行中
                obj.update(update);
                if (event == "addPro") {
                    rowObj = obj;

                    if (rowObj.data["id"] == "") {
                        chooseProduct();
                    }
                    
                } 
                else if (event == 'del') {
                    layer.confirm('确定要删除选中数据？', function (index) {
                        var rsl = DelData(data.id);
                        if (rsl.result) {
                            obj.del();
                            layer.msg("删除成功");
                        } else {
                            layer.msg(info.message);
                        }
                        layer.close(index);

                    });
                }
                
            });

            //监听单元格编辑
            table.on('edit(list)', function (obj) {
                var field = obj.field;
                var data = obj.data;
                var value = obj.value;
                var oldValue = obj.oldValue;

                if (value == "") {
                    value = "-";
                }


                if (field == "number") {
                   
                    if (isNaN(value)) {
                        layer.msg("使用数量必须为数字");
                        data[field] = oldValue;
                        obj.update(data);
                        return;
                     }

                      if (Number(value)<=0) {
                        layer.msg("使用数量需大于0");
                        data[field] = oldValue;
                        obj.update(data);
                        return;
                     }
                    
                    if (value!="" && data.uPrice != "") {
                        //修改金额
                        var total = Number(value) * Number(data.uPrice)
                        //console.log(total)
                        updatePlanMaterials(data.id, "total", total, null, null);
                        updatePlanMaterials(data.id, field, value, null, null);
                    }
                }
                // else if (field == "batch"){
                //     updatePlanMaterials(data.id, field, value, data.proCode, data.number);
                // }
                // else{
                //     updatePlanMaterials(data.id, field, value, null, null);

                // }

            });


        });

    }

    //存货选择
    function chooseProduct() {
        layer.open({
            type: 2,
            maxmin: false,
            fix: false,
            title: "选择存货",
            area: ['90%', '90%'],
            closeBtn: 1,
            shadeClose: true,
            // content: "../Basic/SelectInventory?isSingle=0",
            content: "../Basic/SelectInvStock?isSingle=0",
            //关闭弹出框的回调函数，不管弹出框是手动关闭还是通过脚本进行关闭，都执行
            end: function () {

            }
        });
    }

    //存货参数获取
    function getChildPar(pars) {
        if (pars != undefined) {
            var proCodes = "";
            for (var i = 0; i < pars.length; i++) {
                proCodes += pars[i].proCode + "-" + pars[i].cBatch + ",";
            }
            proCodes = proCodes.slice(0, -1);
            savePlanMaterials(proCodes);
        }

    }

    /**
    * 计划用料数据添加
    * **/
    function savePlanMaterials(proCodes) {
        $.ajax({
            url: "../Plan/savePlanMaterials",
            data: { proCodes: proCodes, plCode: plCode },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    loadPro();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }


    //修改用料
    function updatePlanMaterials(dataId, fields ,value,proCode,number) {

        $.ajax({
            url: "../Plan/updatePlanMaterials",
            data: { dataId: dataId, plCode: plCode, fieldName: fields, fieldValue: value, proCode: proCode,number:number },
            dataType: "json",
            type: "POST",
            success: function (data) {
                if (data.result) {
                    loadPro();
                } else {
                    layer.msg(data.message);
                }
            }
        });
    }

    /**
    * 删除信息
    * **/
    function DelData(dataId) {
        var info = undefined;
        $.ajax({
            url: "../Plan/DelPlanMaterials",
            data: { dataId: dataId },
            dataType: "json",
            type: "POST",
            async: false,
            success: function (rsl) {
                info = rsl;
            }
        });
        return info
    }




</script>

